package config;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;

import javax.sql.DataSource;
import java.beans.PropertyVetoException;
//@Configuration
public class JdbcConfig {
    @Value("${Driver}")
    private  String Driver;
    @Value("${Url}")
    private  String Url;
    @Value("${User}")
    private  String User;
    @Value("${Password}")
    private  String Password;
    /*
     用于创建一个QueryRunner 对象
    * */
    @Bean(name = "runner")
    @Scope("prototype")
    public QueryRunner createQueryRunner(DataSource dataSource){
        return  new QueryRunner(dataSource);
    }
    /*
    用于创建一个数据源对象
    * */
    @Bean(name = "dataSource")
    public  DataSource createDataSource(){
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        try {
            dataSource.setDriverClass(Driver);
            dataSource.setJdbcUrl(Url);
            dataSource.setUser(User);
            dataSource.setPassword(Password);

        } catch (PropertyVetoException e) {
            e.printStackTrace();
        }
        return dataSource;
    }
}
